package com.allwees.bs.c.module.base.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.allwees.core.common.util.DateUtil;
import com.allwees.core.common.util.UuidUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import java.io.Serializable;
import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
@MappedSuperclass
public abstract class JpaBaseEntity implements Serializable {

    @Id
    @TableId(type = IdType.AUTO)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected Integer id;

    @TableField(fill = FieldFill.INSERT)
    @Column(updatable = false)
    protected Date createdAt;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    protected Date updatedAt;

    protected Integer status = 0;

    protected Integer isDeleted = 0;

    @TableField(fill = FieldFill.INSERT, updateStrategy = FieldStrategy.NEVER)
    @Column(updatable = false, length = 64)
    private String uuid;

    //删除时间
    private Date deletedAt;

    private Integer idx = 9999;

    @PrePersist
    public void onCreate() {
        this.createdAt = DateUtil.now();
        this.updatedAt = DateUtil.now();
        this.uuid = UuidUtil.generate();
    }

    @PreUpdate
    public void onUpdate() {
        this.updatedAt = DateUtil.now();
    }
}
